# 字典类型

from flask_jwt_extended import get_jwt_identity

from apps.model.baseModel import BaseModel
from apps.model.serializeModel import model_to_list
from apps.settingApi.api.users.model import usersModel
from comm.sqlalchemyConfig import db


class dictTypeModel(BaseModel):
    """
    字典表
    """
    __tablename__ = 'tx_dict_type'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True,doc='id')
    label = db.Column(db.String(125), nullable=False,unique=True,doc='描述')
    value = db.Column(db.String(125), nullable=False,unique=True,doc='名称值')
    str1 = db.Column(db.String(500), nullable=True, doc='字符1')
    str2 = db.Column(db.String(500), nullable=True, doc='字符2')
    str3 = db.Column(db.String(500), nullable=True, doc='字符3')
    qty1 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字1')
    qty2 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字2')
    qty3 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字3')
    rem1 = db.Column(db.Text, nullable=True,default='',doc='备注1')
    rem2 = db.Column(db.Text, nullable=True,default='',doc='备注2')
    rem3 = db.Column(db.Text, nullable=True,default='',doc='备注3')
    sort = db.Column(db.Integer, nullable=False,default='',doc='排序')
# 字典数据
class dictDataModel(BaseModel):
    """
    字典数据
    """
    __tablename__ = 'tx_dict_data'
    id = db.Column(db.String(125), primary_key=True, autoincrement=False,doc='id')
    # label = db.Column(db.String(125), nullable=False,unique=True,doc='描述')
    label = db.Column(db.String(125), nullable=False,doc='描述')
    value = db.Column(db.String(125), nullable=False,doc='名称值')
    # dict = db.Column(db.String(125), nullable=False, unique=True, doc='名称值')
    dict_value = db.Column(db.String(125), db.ForeignKey('tx_dict_type.value'))
    str1 = db.Column(db.String(500), nullable=True, doc='字符1')
    str2 = db.Column(db.String(500), nullable=True, doc='字符2')
    str3 = db.Column(db.String(500), nullable=True, doc='字符3')
    qty1 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字1')
    qty2 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字2')
    qty3 = db.Column(db.Numeric(20, 6), nullable=True, doc='数字3')
    int1 = db.Column(db.Integer, nullable=True, doc='整数1')
    int2 = db.Column(db.Integer, nullable=True, doc='整数2')
    int3 = db.Column(db.Integer, nullable=True, doc='整数3')
    rem1 = db.Column(db.Text, nullable=True, default='', doc='备注1')
    rem2 = db.Column(db.Text, nullable=True, default='', doc='备注2')
    rem3 = db.Column(db.Text, nullable=True, default='', doc='备注3')
    sort = db.Column(db.Integer, nullable=True, default=0, doc='排序')


    @classmethod
    def getListData(cls, args, filterList, page=1, pageSize=10, excludeFiledList=None):
        """
        重写列表查询
        args url传递的原参数
        filterList 组装过的参数
        page 页码
        pageSize 分页大小
        excludeFiledList 排除字段
        """
        if excludeFiledList is None:
            excludeFiledList = []
        objData = {}
        showFiledList = [
            cls.id,
            cls.label,
            cls.value,
            cls.dict_value,
            cls.rem1,
            cls.rem2,
            cls.rem3,
            cls.sort,
            cls.create_user,
            cls.update_user,
            cls.create_time,
            cls.update_time,
            dictTypeModel.label.label('dict_name')
        ]
        # 解析获取token信息
        current_user = get_jwt_identity()
        userDataList = []
        if current_user['user_type']!=1:
            # 查询管理员账号
            useradmin = db.session.query(usersModel).filter(usersModel.type == 1).all()
            for itm in useradmin:
                userDataList.append(itm.id)
            userDataList.append(current_user['userid'])
            filterList.append(cls.create_user.in_(userDataList))
            # print(filterList)
        modelDataObj = db.session.query(*showFiledList).filter(*filterList,cls.dict_value==dictTypeModel.value).order_by(cls.id.desc()).paginate(
            page=page, per_page=pageSize, error_out=False)
        result = model_to_list(modelDataObj.items, excludeFiledList)
        objData["total"] = modelDataObj.total  # 总记录数
        objData["has_next"] = modelDataObj.has_next  # 是否有下一页
        objData["has_prev"] = modelDataObj.has_prev  # 是否有上一页
        objData["page"] = modelDataObj.page  # 当前页
        objData["pages"] = modelDataObj.pages  # 总页数
        objData["pageSize"] = pageSize  # 分页大小
        objData["data"] = result  # 分页大小
        return objData
